/// Setting up the ANES File for presidential coalitions  

use "/Users/joshuazingher/Desktop/Prospectus /anes_cdfdta/anes coalitions .dta"

sort year

rename VCF0704 presvote

rename VCF0703 voted

rename ADem Adem

gen ADem = Adem/(ARep+Adem)



/// Generating total Union respondants by year

by year: egen unionresp = count(Union) if Union == 1

/// Generating total number of Union voters by year


by year: egen Unionvoted = count(voted) if Union==1 & voted==3

/// Generating total number of Latnio non-voters by year

by year: egen UnionNvoted = count(voted) if voted==0 & Union==1 | voted==1 & Union==1 | voted==2 & Union==1

/// Generating total number of non-Union voters by year

by year: egen NUnionvoted = count(voted) if Union~=1 & voted==3

/// Generating total number of non-Union non-voters by year

by year: egen NUnionNvoted = count(voted) if Union~=1 & voted==0 | Union~=1 & voted==1 | Union~=1 & voted==2

/// filling in the blanks

by year: egen unionvoted = max(Unionvoted)
by year: egen unionNvoted = max(UnionNvoted)
by year: egen Nunionvoted = max(NUnionvoted)
by year: egen NunionNvoted = max(NUnionNvoted)




/// Generating % Union TO with Mosteller Correction

gen N = 1
gen a = [unionvoted * NunionNvoted] / [unionNvoted * Nunionvoted]


///Generating filler vars

gen aminone =(a-1)
gen AUnionplusTO = (AUnion+aTO)
gen aUnionTO=(a*AUnion*aTO)

gen CUnionTO = 1/ (2*(aminone)) * (N+(aminone*AUnionplusTO) - sqrt((N+aminone*AUnionplusTO)*(N+aminone*AUnionplusTO) - (4*(aminone*aUnionTO))))

///Adjusting TO for the formula

replace CUnionTO = CUnionTO/AUnion



/// Moving to Partisanship - Generating Union Democratic Vote total by year

by year: egen Uniondem = count(Union) if presvote==1 & Union==1

/// Generating Union Non-Democratic Vote by Year

by year: egen UnionNdem = count(Union) if presvote==2 & Union==1

// Generating non-Union Democratic Vote total by year

by year: egen NUniondem = count(Union) if presvote==1 & Union~=1

/// Generating non-Union non-Democratic vote total by year

by year: egen NUnionNdem = count(Union) if presvote==2 & Union~=1

/// filling in the blanks

by year: egen unionDem = max(Uniondem)
by year: egen unionNDem = max(UnionNdem)
by year: egen NunionDem = max(NUniondem)
by year: egen NunionNDem = max(NUnionNdem)

/// Generation % Union Democrat with Mosteller Correction

gen A = (unionDem*NunionNDem)/(unionNDem*NunionDem)
gen Aminone =(A-1)
gen AUnionplusDem = (AUnion+ADem)
gen AUnionDem=(A*AUnion*ADem)
gen CUnionDem = 1/(2*(Aminone)) * (N+(Aminone*AUnionplusDem) - sqrt((N+Aminone*AUnionplusDem)*(N+Aminone*AUnionplusDem) - (4*(Aminone*AUnionDem))))

replace CUnionDem = CUnionDem/AUnion


/// Generating number of ANES respondents by year

by year: egen totalresp = count (VCF0006a)


/// Coalition Contribution

gen unioncoalcontrib = (CUnionTO* CUnionDem *AUnion)/(aTO*ADem)



